<template>
    <div class="error-container">
        <div class="error-content">

            <div class="error-icon">
                <el-icon :size="80" class="error-icon-large">
                    <WarningFilled v-if="errorType === 'warning'" />
                    <CircleCloseFilled v-else-if="errorType === 'error'" />
                    <HelpFilled v-else />
                </el-icon>
            </div>

            <h1 class="error-title">{{ errorTitle }}</h1>

            <div class="error-details">
                <div class="error-info-item">
                    <span class="error-label">错误类型：</span>
                    <span class="error-value">{{ errorTypeText }}</span>
                </div>

                <div class="error-info-item" v-if="errorMessage">
                    <span class="error-label">错误描述：</span>
                    <span class="error-value error-message">{{ errorMessage }}</span>
                </div>

                <div class="error-info-item" v-if="errorCode">
                    <span class="error-label">错误代码：</span>
                    <span class="error-value error-code">{{ errorCode }}</span>
                </div>

                <div class="error-info-item" v-if="errorStack && isDevMode">
                    <span class="error-label">错误堆栈：</span>
                    <pre class="error-stack">{{ errorStack }}</pre>
                </div>
            </div>

            <div class="error-actions">
                <el-button type="primary" @click="goToHome" class="primary-button">
                    <el-icon>
                        <House />
                    </el-icon> 返回首页
                </el-button>
                <el-button @click="goBack" v-if="canGoBack">
                    <el-icon>
                        <ArrowLeft />
                    </el-icon> 返回上一页
                </el-button>
                <el-button @click="reloadPage">
                    <el-icon>
                        <Refresh />
                    </el-icon> 重新加载
                </el-button>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { WarningFilled, CircleCloseFilled, HelpFilled, House, ArrowLeft, Refresh } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

// 响应式数据
const errorType = ref('error') // 默认错误类型
const errorMessage = ref('')
const errorCode = ref('')
const errorStack = ref('')
// 是否为开发模式
const isDevMode = import.meta.env.DEV;

// 计算属性
const errorTitle = computed(() => {
    switch (errorType.value) {
        case 'warning':
            return '警告'
        case 'error':
            return '出错了'
        case 'not-found':
            return '页面不存在'
        default:
            return '发生错误'
    }
})

const errorTypeText = computed(() => {
    const typeMap: Record<string, string> = {
        'warning': '警告',
        'error': '错误',
        'not-found': '页面不存在',
        'network': '网络错误',
        'server': '服务器错误',
        'permission': '权限不足'
    }
    return typeMap[errorType.value] || '未知错误'
})

const canGoBack = computed(() => {
    return window.history.length > 1
})

const goToHome = () => {
    router.push('/')
}

const goBack = () => {
    router.back()
}

const reloadPage = () => {
    window.location.reload()
}

// 从路由参数中获取错误信息
const loadErrorInfo = () => {
    const query = route.query

    // 从路由参数中读取错误信息
    if (query.type) {
        errorType.value = String(query.type)
    }
    if (query.message) {
        errorMessage.value = String(query.message)
    }
    if (query.code) {
        errorCode.value = String(query.code)
    }
    if (query.stack) {
        errorStack.value = String(query.stack)
    }

    // 对于404等路由未匹配的情况
    // 检测是否是通过NotFound路由或未匹配路由进来的
    const isNotFoundRoute = route.name === 'NotFound' || route.matched.length === 0
    if (isNotFoundRoute && !query.type) {
        errorType.value = 'not-found'
        errorMessage.value = '您访问的页面不存在或已被移除'
        errorCode.value = '404'
    }
}

// 组件挂载时加载错误信息
onMounted(() => {
    loadErrorInfo()
})

// 暴露给全局使用的错误处理方法
defineExpose({
    // 可以通过调用此方法更新错误信息
    updateErrorInfo(info: {
        type?: string,
        message?: string,
        code?: string,
        stack?: string
    }) {
        if (info.type) errorType.value = info.type
        if (info.message) errorMessage.value = info.message
        if (info.code) errorCode.value = info.code
        if (info.stack) errorStack.value = info.stack
    }
})
</script>

<style scoped>
.error-container {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #f5f7fa;
    padding: 20px;
}

.error-content {
    background-color: #ffffff;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    padding: 48px;
    max-width: 600px;
    width: 100%;
    text-align: center;
    transition: all 0.3s ease;
}

.error-content:hover {
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

.error-icon {
    margin-bottom: 24px;
}

.error-icon-large {
    color: var(--el-color-danger);
}

.error-title {
    font-size: 28px;
    font-weight: 600;
    color: #303133;
    margin-bottom: 32px;
}

.error-details {
    margin-bottom: 40px;
    text-align: left;
    border-radius: 8px;
    background-color: #f9f9f9;
    padding: 24px;
}

.error-info-item {
    display: flex;
    margin-bottom: 16px;
    align-items: flex-start;
}

.error-info-item:last-child {
    margin-bottom: 0;
}

.error-label {
    font-weight: 600;
    color: #606266;
    width: 80px;
    flex-shrink: 0;
}

.error-value {
    color: #303133;
    flex: 1;
}

.error-message {
    word-break: break-word;
}

.error-code {
    font-family: 'Courier New', monospace;
    background-color: #eef2ff;
    color: #4361ee;
    padding: 2px 8px;
    border-radius: 4px;
}

.error-stack {
    background-color: #f0f0f0;
    padding: 12px;
    border-radius: 6px;
    font-family: 'Courier New', monospace;
    font-size: 12px;
    overflow-x: auto;
    color: #606266;
    max-height: 200px;
    overflow-y: auto;
    margin: 8px 0 0 0;
    width: 100%;
    text-align: left;
}

.error-actions {
    display: flex;
    justify-content: center;
    gap: 12px;
    flex-wrap: wrap;
}

.primary-button {
    min-width: 120px;
}

@media (max-width: 768px) {
    .error-content {
        padding: 32px 24px;
    }

    .error-title {
        font-size: 24px;
    }

    .error-info-item {
        flex-direction: column;
        align-items: flex-start;
    }

    .error-label {
        margin-bottom: 4px;
    }

    .error-actions {
        flex-direction: column;
    }

    .error-actions button {
        width: 100%;
    }
}
</style>
